package com.iap.admin.mediator
{
	import com.iap.admin.event.ImageEvent;
	import com.iap.admin.signal.ImageRequestSignal;
	import com.iap.admin.view.filler.FillerImageControl;
	import com.iap.app.AppGlobal;
	import com.iap.app.StatusCode;
	import com.iap.app.model.ServiceResponse;
	import com.iap.app.translator.ServiceTranslator;
	import com.iap.app.util.logger.Log;
	import com.iap.common.model.Filler;
	import com.iap.common.model.Image;
	import com.iap.common.translator.ImageTranslator;
	
	import flash.events.DataEvent;
	import flash.events.Event;
	import flash.events.IEventDispatcher;
	import flash.events.IOErrorEvent;
	import flash.events.ProgressEvent;
	import flash.net.FileFilter;
	import flash.net.FileReference;
	import flash.net.URLRequest;
	import flash.net.URLVariables;
	import com.iap.common.mediator.BaseMediator;

	public class FillerImageControlMediator extends BaseMediator
	{
		[Inject]
		public var view:FillerImageControl;
		[Inject]
		public var request:ImageRequestSignal;

		protected var model:Image;

		protected var file:FileReference;
		
		override public function onRegister():void
		{
			view.onAddButtonClick.add(addImageHandler);
			view.onSetSource.add(setSourceHandler);
			addContextListener(ImageEvent.RESPONSE, responseHandler);
		}

		private function setSourceHandler(value:Image):void
		{
			model = value;
		}

		private function addImageHandler(value:String):void
		{
			if(value == "add")
				browseFile();
		}
		
		protected function browseFile():void
		{
			file ||= new FileReference();
			addListeners(file);
			file.browse(getTypes());
		}
		
		private function addListeners(dispatcher:IEventDispatcher):void
		{
			dispatcher.addEventListener(DataEvent.UPLOAD_COMPLETE_DATA, uploadDataComplete);
			dispatcher.addEventListener(IOErrorEvent.IO_ERROR, ioErrorHandler);
			dispatcher.addEventListener(ProgressEvent.PROGRESS, progressHandler);
			dispatcher.addEventListener(Event.SELECT, selectHandler);
		}
		
		private function removeListeners(dispatcher:IEventDispatcher):void
		{
			dispatcher.removeEventListener(DataEvent.UPLOAD_COMPLETE_DATA, uploadDataComplete);
			dispatcher.removeEventListener(IOErrorEvent.IO_ERROR, ioErrorHandler);
			dispatcher.removeEventListener(ProgressEvent.PROGRESS, progressHandler);
			dispatcher.removeEventListener(Event.SELECT, selectHandler);
		}

		private function selectHandler(event:Event):void
		{
			deletePreviousImage();
			sendFile();
		}
		
		private function deletePreviousImage():void
		{
			if(model == null) return;
			request.dispatch(model, ImageRequestSignal.DELETE);
		}
		
		private function sendFile():void
		{
			var request:URLRequest = new URLRequest();
			request.url = AppGlobal.FileUploadEndPoint;
			file.upload(request,"file");
		}
		
		private function uploadDataComplete(evt:DataEvent):void
		{
			removeListeners((evt.target as FileReference));
			var serviceRespones:ServiceResponse = ServiceTranslator.translate(evt.data);
			var result:Image = ImageTranslator.translate(serviceRespones.result) as Image;
			view.progressBar.visible = false;
			view.onImageAdded.dispatch(result); 
		}

		private function ioErrorHandler(event:IOErrorEvent):void
		{
			removeListeners(file);
			Log.Info("ioErrorHandler: " + event + " url="+AppGlobal.FileUploadEndPoint);
			view.progressBar.visible = false;
		}
		
		private function progressHandler(event:ProgressEvent):void
		{
			view.progressBar.visible = true;
		}
		
		protected function getTypes():Array
		{
			var allTypes:Array = new Array(new FileFilter
				(
					"Images (*.jpg; *.jpeg; *.gif; *.png)",
					"*.jpg; *.jpeg; *.gif; *.png; *.tiff; *.tif;"
				));
			return allTypes;
		}
		
		private function responseHandler(evt:ImageEvent):void
		{
			AppGlobal.endWait();
			if(evt.response.status.code == StatusCode.OK)
			{
				
			}
		}
	}
}